<template>
	<view>
		<u-top-tips ref="uTips"></u-top-tips>
		<u-form style="background-color: #FFFFFF;padding: 20rpx;" :model="updateActivity" :rules="rules" ref="uForm"
			:errorType="errorType">
			<u-form-item :leftIconStyle="{color: '#888', fontSize: '32rpx'}" label-width="180"
				label-position="left" label="活动名称" prop="title">
				<u-input :border="border" placeholder="请输入活动名称" v-model="updateActivity.title" type="text"></u-input>
			</u-form-item>
			<u-form-item :leftIconStyle="{color: '#888', fontSize: '32rpx'}" label-width="180"
				label-position="left" label="副标题" prop="subTitle">
				<u-input :border="border" placeholder="请输入副标题" v-model="updateActivity.subTitle" type="text"></u-input>
			</u-form-item>
			<u-form-item v-if="updateActivity.image==null" label-position="left" label="上传封面" prop="logo"
				label-width="180">
				<u-upload ref="uUpload" :custom-btn="false" :show-upload-list="true"
					:auto-upload="false" :show-progress="true" :deletable="true" :max-count="1"
					@on-list-change="onListChange">
				</u-upload>
			</u-form-item>
			<u-form-item :style="{color:timeIsFail?'red':'#000000'}" label-position="left" label="活动时间"
				prop="createTime" label-width="180">
				<view class="u-demo-result-line" style="border: 1rpx solid #000000;border-radius: 20rpx;padding: 0 20rpx;" @click="showStartTimePicker=true">
					{{ updateActivity.createTime ? updateActivity.createTime : "选择开始时间" }}
				</view>
				<u-picker mode="time" :defaultTime="updateActivity.createTime ? updateActivity.createTime : defaultTime"
					v-model="showStartTimePicker" :params="startTime" end-year="2030" @confirm="startTimePickerConfirm">
				</u-picker>
				<u-icon style="margin: 0 30rpx;" name="arrow-right-double" size="40" color="#909399"></u-icon>
				<view class="u-demo-result-line" style="border: 1rpx solid #000000;border-radius: 20rpx;padding: 0 20rpx;" @click="showEndTimePicker=true">
					{{ updateActivity.endTime ? updateActivity.endTime : "选择结束时间" }}
				</view>
				<u-picker mode="time" :defaultTime="updateActivity.endTime ? updateActivity.endTime : defaultTime"
					v-model="showEndTimePicker" :params="endTime" end-year="2030" @confirm="endTimePickerConfirm">
				</u-picker>
			</u-form-item>

			<u-form-item label-position="left" label="活动详情" prop="detail" label-width="180">
				<u-input type="textarea" :border="border" placeholder="请填写活动详情" v-model="updateActivity.detail" />
			</u-form-item>
			<u-form-item :leftIconStyle="{color: '#888', fontSize: '32rpx'}" label-width="180"
				label-position="left" label="最大人数" prop="joinmax">
				<u-input :border="border" placeholder="请输入最大参与人数" v-model="updateActivity.joinmax" type="number">
				</u-input>
			</u-form-item>
			<u-form-item :rightIconStyle="{color: '#888', fontSize: '32rpx'}" left-icon="account"
				label-position="left" label="手机号码" prop="contact" label-width="180">
				<u-input :border="border" placeholder="请输入手机号" v-model="updateActivity.contact" type="number"></u-input>
			</u-form-item>
		</u-form>
		<u-button
			style="font-size: 24rpx;letter-spacing:10rpx;height: 80rpx;line-height: 80rpx;font-size: 32rpx;font-weight: 900;margin-top: 30rpx;"
			@click="submit" shape="square" size="default" :ripple="true" type="default">
			提交
		</u-button>
		<u-button
			style="font-size: 24rpx;letter-spacing:10rpx;height: 80rpx;line-height: 80rpx;font-size: 32rpx;font-weight: 900;margin-top: 30rpx;"
			@click="close" shape="square" size="default" :ripple="true" type="default">
			返回
		</u-button>
	</view>
</template>

<script>
	export default {
		props: ["caozuo", "updateActivity","nowUserClub"],
		data() {
			return {
				border: false,
				labelPosition: 'left',
				errorType: ['message'],
				rules: {
					title: [{
							required: true,
							message: '请输入活动名称',
							trigger: 'blur',
						},
						{
							min: 5,
							message: '活动名称不能少于5个字',
							trigger: 'change',
						},
					],
					subTitle: [],
					detail: [{
							required: true,
							message: '请填写简介',
							trigger: 'blur',
						},
						{
							min: 5,
							message: '简介不能少于5个字',
							trigger: 'change',
						}
					],
					joinmax: [{
						required: true,
						message: '请填写最大参与人数',
						trigger: 'blur',
					}],
					contact: [{
							required: true,
							message: '请输入联系方式',
							trigger: ['change', 'blur'],
						},
						{
							validator: (rule, value, callback) => {
								// 调用uView自带的js验证规则，详见：https://www.uviewui.com/js/test.html
								return this.$u.test.mobile(value);
							},
							message: '手机号码不正确',
							// 触发器可以同时用blur和change，二者之间用英文逗号隔开
							trigger: ['change', 'blur'],
						}
					]
				},

				defaultTime: '2019-12-11',
				showStartTimePicker: null,
				showEndTimePicker: null,
				startTime: {
					year: true,
					month: true,
					day: true,
				},
				endTime: {
					year: true,
					month: true,
					day: true,
				},
				timeIsFail: false,

				lists: [],
			}
		},
		onLoad() {
			// console.log("当前社团", this.updateActivity)
			// console.log("当前操作", this.caozuo)
			// this.updateActivity = this.updateActivity
			this.defaultTime = ""
			var date = new Date()
			this.defaultTime = this.defaultTime + date.getFullYear() + "-" + (parseInt(date.getMonth()) + 1) + "-" + date
				.getDate();
			console.log(this.defaultTime)
			if(this.updateActivity.createTime){
				this.updateActivity.createTime = this.defaultTime
				this.updateActivity.endTime = this.defaultTime
			}
			console.log("社团id为",this.nowUserClub)
		},
		methods: {
			showTips(duration, title, type) {
				this.$refs.uTips.show({
					duration: duration,
					title: title,
					type: type
				});
			},
			submit() {
				var that = this
				if (this.timeIsFail) {
					this.showTips(2000, "时间设置有误", "error")
					console.log("时间有误")
					return;
				}
				//新增
				if(this.updateActivity.image==null){
					uni.uploadFile({
						url: this.url.file.upload, //仅为示例，非真实的接口地址
						method: 'POST',
						filePath: this.lists[0].url,
						name: 'file',
						success: (res) => {
							console.log(res)
							var data = JSON.parse(res.data);
							if(data.msg == "success"&& data.url!=null) {
								this.updateActivity.image = data.url
								this.updateActivity.clubId = this.nowUserClub.clubId
								this.updateActivity.clubName = this.nowUserClub.clubName
								console.log(this.updateActivity)
								//新增
								this.saveMovement(this.updateActivity);
							}
						},
						fail: (res) => {
							console.log(res)
						}
					});
				}else{
					//修改
					this.saveMovement(that.updateActivity);
				}
			},
			saveMovement(updateActivity){
				uni.request({
					url: this.url.movement.saveMovement,
					method: 'POST',
					data: JSON.stringify(updateActivity),
					header: {
						"content-type": "application/json;charset=UTF-8"
					},
					success: (res) => {
						console.log(res)
						if (res.statusCode == 200) {
							that.close();
						}
					},
					fail: (res) => {
						console.log("失败！")
					}
				});
			},
			close() {
				this.$emit("updateOk");
			},
			startTimePickerConfirm(e) {
				this.updateActivity.createTime = ""
				if (this.startTime.year) this.updateActivity.createTime += e.year;
				if (this.startTime.month) this.updateActivity.createTime += '-' + e.month;
				if (this.startTime.day) this.updateActivity.createTime += '-' + e.day;
				// this.updateActivity.createTime = (new Date(Date.parse(this.updateActivity.createTime)))
				this.timeIsFail = this.panduanTimeIsFail()
			},
			endTimePickerConfirm(e) {
				this.updateActivity.endTime = ""
				if (this.endTime.year) this.updateActivity.endTime += e.year;
				if (this.endTime.month) this.updateActivity.endTime += '-' + e.month;
				if (this.endTime.day) this.updateActivity.endTime += '-' + e.day;
				// this.updateActivity.endTime = (new Date(Date.parse(this.updateActivity.endTime)))
				this.timeIsFail = this.panduanTimeIsFail()
				console.log(this.timeIsFail)
			},
			panduanTimeIsFail() {
				var startTime = String(this.updateActivity.createTime).split("-")
				var endTime = String(this.updateActivity.endTime).split("-")
				console.log(endTime)
				if (parseInt(endTime[0]) < parseInt(startTime[0])) {
					console.log(endTime[0], startTime[0])
					return true;
				}
				if (parseInt(endTime[0]) == parseInt(startTime[0])) {
					if (parseInt(endTime[1]) < parseInt(startTime[1])) {
						console.log(endTime[1], startTime[1])
						return true;
					}
					if (parseInt(endTime[1]) == parseInt(startTime[1])) {
						if (parseInt(endTime[2]) < parseInt(endTime[2])) {
							console.log(endTime[2], startTime[2])
							return true;
						}
					}
				}
				return false;
			},

			//选择图片
			onListChange(lists) {
				this.lists = lists;
				console.log("进入集合")
				console.log(this.lists)
			}
		}
	}
</script>

<style>

</style>
